'''
    Module used to fetch SOA (domain and IP) records for the given target domain
'''
import utils
from scapy.all import IP, UDP, DNS, DNSQR, sr1
from argparse import Namespace

def expandLayers(x, payload):
    yield getattr(x, payload)
    while x.payload:
        x = x.payload
        yield getattr(x, payload)

def getSoaForDomain(args):
    pkt = IP(dst="8.8.8.8") / UDP(sport=utils.getRandomPort()) / DNS(qr=0, rd=1, qd=DNSQR(qname=args.targetDomain, qtype="NS"))
    ans = sr1(pkt, verbose=False)
    args.soaDomain = list(expandLayers(ans[DNS].an, "rdata"))

    args.soaIP = list()
    for domain in args.soaDomain:
    pkt = IP(dst="8.8.8.8") / UDP(sport=utils.getRandomPort()) / DNS(qd=DNSQR(qname=domain, qtype="A"))
        ans = sr1(pkt, verbose=False)
        args.soaIP.append(ans[DNS].an.rdata)

    print args
